
//Belegt Pin 2 mit interrupt und updated die Variable "aktSpeed"


//******* Bitte einstellen ********
const unsigned int radumfang = 2410; //in mm
//*********************************

float aktSpeed = 0.0;
unsigned int lastChangeTime = 0;
unsigned int lastCount = 0;
volatile unsigned int count = 0;  

void setupSpeedSensor(){
  attachInterrupt(0, isr, RISING);
  aktSpeed = 0.0;
  lastCount=0;
  count=0;
}

void updateSpeed()
{
  //ist der Magnet am Sensor vorbeigekommen?
  if ((count!=lastCount) && ((myMillis() - lastChangeTime)>150)){
    
    Serial.print("aktuelle Zeit:");Serial.println(myMillis());
    float t = (float)(myMillis() - lastChangeTime) / 1000.0; //Zeit in s
    Serial.print("t=");Serial.println(t,2);
    float s = radumfang / 1000.0; //Strecke in m
    Serial.print("s=");Serial.println(s,2);
    float faktor = 1.0/t; //faktor um von x Sekunden auf 1 Sekunde zu kommen
    t = t * faktor;
    s = s * faktor;
    Serial.print("faktor=");Serial.println(faktor,2);
    float v = s/t; //Speed in m/s
    aktSpeed = v * 3.6; //Speed in km/h
    
    Serial.print("t=");Serial.print(t,2);Serial.print(" sek");Serial.print("\t");
    Serial.print("s=");Serial.print(s,2);Serial.print(" meter");Serial.print("\t");
    Serial.println(" ");
    Serial.print("v(m/s)=");Serial.println(v,2);
    Serial.print("aktSpeed(km/h)=");Serial.println(aktSpeed,2);
    Serial.println(" ");
    
    //Überlauf vermeiden
    if (count>65000){
      count = 0; lastCount = 0;
    }
    
    lastChangeTime = myMillis();
  }  
  lastCount = count;
}
  
void isr()
{
  count++;
}






































